package com.example.i18n.controller;

import com.example.i18n.vo.MsgVO;
//import com.example.validated.vo.BaseDto;
//import com.example.validated.vo.I18nVO;
//import com.example.validated.vo.WebControllerUtil;
import com.example.i18n.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Locale;

@Validated
@RestController
@RequestMapping("/i18n")
public class I18nController {

    @Autowired
    private MessageSource messageSource;

    @PostMapping("/msg")
    public String user(@RequestBody @Valid MsgVO i18nVO) {
        Locale locale = LocaleContextHolder.getLocale();
        return "hhh";
    }

    @PostMapping("/userInfo")
    public String user(@RequestBody @Valid UserInfoVO userInfoVO) {
        String message = messageSource.getMessage("name.notBlank", null, LocaleContextHolder.getLocale());
        return message;
    }

    @GetMapping("/custom")
    public String custom(HttpServletRequest request) {
        Locale locale = request.getLocale();
        String message = messageSource.getMessage("name.notBlank", null, LocaleContextHolder.getLocale());
        return message;
    }

//    @RequestMapping(value = "/addUser")
//    @ResponseBody
//    public BaseDto addUser(@RequestBody @Validated I18nVO vo, BindingResult bindingResult, HttpServletRequest request) {
//        WebControllerUtil.buildErrorMsg(bindingResult, request);
//        return new BaseDto(true);
//    }
}
